import 'dart:async';
import 'dart:collection';

import 'package:bloc/bloc.dart';
import 'package:hive/hive.dart';
import 'package:meta/meta.dart';

import '../../api/api_client.dart';
import '../../models/services_data.dart';
import '../../models/user.dart';

part 'service_event.dart';
part 'service_state.dart';

class ServiceBloc extends Bloc<ServiceEvent, ServiceState> {

  final ApiClient apiClient;
  late final Box<User> userBox;

  ServiceBloc({required this.apiClient}) : super(ServiceInitial()) {
    userBox = Hive.box<User>('user_box');
    on<ServiceEventInit>(_handleServiceEvent);
    on<ServiceOrderListEventInit>(_handleOrderListEvent);
    on<ServiceOrderBuyEven>(_buyServiceEvent);
    on<ServiceCheckChatEvent>(_checkChatServiceEvent);
  }

  FutureOr<void> _handleServiceEvent(ServiceEventInit event, Emitter<ServiceState> emit) async {
    try {
      var user = userBox.get('currentUser');
      var id = user?.id ?? '';
      var companyId = user?.appCompanyDto?.id ?? '';
      var response = await apiClient.findService(id,companyId);
      if(response.isSuccessful){
        emit(ServiceSetSuccessState(response.data));
      }else{
        emit(ServiceNoDataState());
      }
      print('object ${response?.data?.length}');
      print('object ${response.data?.map((e) => e.serviceName)}');
    } catch (e) {
      emit(ServiceNoDataState());
      print('object 服务列表异常 $e ');

    }
  }


  int page = 0;
  int size = 10;
  FutureOr<void> _handleOrderListEvent(ServiceOrderListEventInit event, Emitter<ServiceState> emit) async {
    try {
      var user = userBox.get('currentUser');

      var id = user?.id ?? '';
      var companyId = user?.appCompanyDto?.id ?? '';
      var response = await apiClient.ordersList(id);
      var datalist = response.data?.content??[];

      print('object $datalist');
      print('object ${datalist.length}');

      if(response.isSuccessful){
        emit(ServiceOrderListSuccessState(datalist));
      }else{
        emit(ServiceNoDataState());
      }
    } catch (e) {
      emit(ServiceNoDataState());
    }
  }



  FutureOr<void> _buyServiceEvent(ServiceOrderBuyEven event, Emitter<ServiceState> emit) async {
    try {
      emit(ServiceLoadingState());
      var user = userBox.get('currentUser');
      event.params['userId'] = user?.id;
      event.params['companyId'] = user?.appCompanyDto?.id ?? '';
      var response = await apiClient.serviceOrdersCreate(event.params);
      if(response.isSuccessful){
        emit(ServiceBuySuccessState(response.data));
      }else{
        emit(ServiceErrorState(response?.message ?? ''));
      }
      print('object ${response?.data}');

    } catch (e) {
      emit(ServiceNoDataState());
      print('object 服务列表异常 $e ');

    }
  }




  FutureOr<void> _checkChatServiceEvent(ServiceCheckChatEvent event, Emitter<ServiceState> emit) async {
    print('object ----------------------------------------                 ');

    try {
      emit(ServiceLoadingState());
      var user = userBox.get('currentUser');

      var params = HashMap<String, dynamic>();
       params['userId'] = user?.id;
       params['companyId'] = user?.appCompanyDto?.id ?? '';
      var response = await apiClient.ordersConsultEnable(params );


      print('object ${params}');
      print('object ${response?.data}');



      if(response.isSuccessful){
        emit(ServiceCanChatState());
      }else{
        emit(ServiceChatErrorState(response?.message ?? ''));
      }
      print('object ${response?.data}');

    } catch (e) {
      emit(ServiceChatErrorState(e.toString()));
      print('object 服务列表异常 $e ');

    }
  }





}
